<?php
namespace Api\Controller;

use Think\Controller;

class ApiController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 注册短信发送
     * @return [type] [description]
     */
    public function sms()
    {
        $mobile = I('post.mobile', '', 'trim');

        if (!is_phone($mobile)) {
            error(8086, '手机号码格式错误');
        }

        $conf = C('SMS.' . C('SMS_TYPE'));

        Vendor('Sms.' . C('SMS_TYPE'));
        $sms = new \Sms($conf['key']);

        // 生成验证码
        $code = rand_number(0, 9999);

        // 发送验证码
        $data['apikey'] = $conf['key'];
        $data['text']   = $text   = "【闪风猫】您的验证码是" . $code . "。如非本人操作，请忽略本短信";
        $data['mobile'] = $mobile;
        $result         = $sms->send($data);

        // 数据返回
        if ($result['code'] == 0) {
            S('sms_' . $mobile, $code, 60 * 5);
            $flag  = true;
            $staus = 1;
        } else {
            $flag  = false;
            $staus = 0;
        }

        // 日志记录
        D('Api/Sms')->log(C('SMS_TYPE'), $mobile, $text, $staus, json_encode($result));

        if ($flag) {
            success();
        } else {
            error(8087, '发送失败');
        }
    }

    /**
     * 省市区全部信息
     * @return [type] [description]
     */
    public function area()
    {
        $area = D('Api/Area', 'Service')->detail();
        success($area);
    }

    /**
     * 获取省信息
     * @return [type] [description]
     */
    public function province()
    {
        $provinces = D('Api/Area', 'Service')->province();
        success($provinces);
    }

    /**
     * 获取城市信息
     * @return [type] [description]
     */
    public function city()
    {
        $province_id = I('post.id', 0, 'intval');

        $ciyts = D('Api/Area', 'Service')->city($province_id);

        success($ciyts);
    }

    /**
     * 获取区县信息
     * @return [type] [description]
     */
    public function region()
    {
        $city_id = I('post.id', 0, 'intval');

        $regions = D('Api/Area', 'Service')->region($city_id);

        success($regions);
    }
}
